﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using VietPublisher.Membership;
using VietPublisher.API.Membership;
using VietPublisher.BSL.BK;
using System.Transactions;
using System.Web.Security;
using VietPublisher.FileManager;
using VietPublisher.Repository.Repositories.Concrete;
using VietPublisher.BaseProject.Models.cart;
using VietPublisher.BaseProject.Models;
using System.Web;
using VietPublisher.BSL.BK;
using VietPublisher.BaseProject.Utils.Miscellaneous;





namespace VietPublisher.Repositories.Interface
{
    public class CartRepository : BaseRepository, ICartRepository
    {        

        public CartRepository()
        {
            //HttpContext.Current.Session["SessionCartHelper"] = "";            
        }

        public Cart GetCart()
        {
            return ShoppingCart;
        }

        public Cart UpdateQuantity(int bookID, int quantity)
        {
            int quantityInStock = GetQuantityByBookId(bookID);
            Cart cart = new Cart();
            if (quantity <= quantityInStock)
            {
                cart = ShoppingCart;
                cart.UpdateQuantity(bookID, quantity);
                //update cart session
                ShoppingCart = cart;                
            }
            return ShoppingCart;
        }

        public Cart AddToCart(int bookID, int quantity)
        {
            Cart cart = new Cart();
            cart = ShoppingCart;
            CartItem cartItem = GetCartItem(bookID);            


            if (ShoppingCart.items.Count > 0 && cart.isBookExists(bookID))
            {
                cart = ShoppingCart;
                cartItem.Quantity += quantity;
                cart.UpdateQuantity(cartItem.BookID, cartItem.Quantity);
            }
            else
            {
                cartItem = GetNewCartItem(bookID);
                cart.AddToCart(cartItem);                                
            }

            ShoppingCart = cart;
            return ShoppingCart;
        }

        public Cart EmptyCart()
        {
            //initial of Cart
            Cart cart = new Cart();

            //Empty a cart
            cart.EmptyItems();

            //set session["cart"] again
            ShoppingCart = cart;
           
            return ShoppingCart;
        }


        public int GetQuantityByBookId(int bookID)
        {
            int quantityInStock = 0;

            //get Book in T_BK_Book table
            var book = (
                        from bk in ContextBookStore.T_BK_Book
                        where bk.BookID == bookID
                        select bk
                       )
                       .FirstOrDefault();

            if (book != null)
            {
                quantityInStock = book.Quantity.Value;
            }

            return quantityInStock;
        }


        public Cart RemoveCartItem(int bookID)
        {
            Cart cart = ShoppingCart;
            CartItem cartItem = GetCartItem(bookID);
            cart.RemoveItem(cartItem);
            ShoppingCart = cart;
            return ShoppingCart;
        }


        public CartItem GetCartItem(int bookID)
        {
            CartItem cartItem =  GetCart().GetCartItemFromBookID(bookID);            
            return cartItem;
        }

        public CartItem GetNewCartItem(int bookID)
        {
            IBookRepository RepoBook = new BookRepository();
            T_BK_Book bk = RepoBook.GetBookByBookID(bookID);
            CartItem cartItem = new CartItem()
            {
                BookCode = bk.BookCode,
                BookID = bk.BookID,
                ItemName = bk.BookTitle,
                Price = bk.PriceCurrent.Value,
                Quantity = 1,
                Total = bk.PriceCurrent.Value
            };        
            return cartItem;
        }
    }
}
